jQuery(function ($) {


    $(".email").blur(function () {
        var reg = /^[0-9A-Za-z][\.-_0-9A-Za-z]*@[0-9A-Za-z]+(?:\.[0-9A-Za-z]+)+$/;
        var email = $(".email").val();
        if (reg.test(email) === false) {
            alert("email有误，请重填");
            return false;
        }
    })

    $(".bankAccount").blur(function () {
        var reg = /^(\d{16}|\d{19})$/;
        var bankAccount = $(".bankAccount").val();
        if (reg.test(bankAccount) === false) {
            alert("银行卡号码有误，请重填");
            return false;
        }
    })


    $(".urgentContacts").blur(function () {
        var phone = $(".urgentContacts").val();
        if (!(/^1(3|4|5|7|8)\d{9}$/.test(phone))) {
            alert("紧急联系号码有误，请重填");
            return false;
        }
    })

    $(".phone").blur(function () {
        var phone = $(".phone").val();
        if (!(/^1(3|4|5|7|8)\d{9}$/.test(phone))) {
            alert("手机号码有误，请重填");
            return false;
        }
    })

    $(".isCardNo").blur(function () {
        var card = $(".isCardNo").val();
        // 身份证号码为15位或者18位，15位时全为数字，18位前17位为数字，最后一位是校验位，可能为数字或字符X
        var reg = /(^\d{15}$)|(^\d{18}$)|(^\d{17}(\d|X|x)$)/;
        if (reg.test(card) === false) {
            alert("身份证输入不合法");
            return false;
        }
    })

    var status = $("#status");
    var sValue = $("#sValue").val();
    $.ajax({
        type: "GET",
        url: "/sys/archives/status",
        dataType: "json",
        success: function (data) {
            if (data != null) {
                status.html("");
                var options = "<option value=\"0\">请选择</option>";
                for (var i = 0; i < data.length; i++) {
                    if (sValue == data[i].id) {
                        options += "<option value=\"" + data[i].id + "\" selected >" + data[i].valueName + "</option>";
                    } else {
                        options += "<option value=\"" + data[i].id + "\" >" + data[i].valueName + "</option>";
                    }
                }
                status.html(options);
            }
        },
        error: function (data) {
            //validateTip(booktype.next(),{"color":"red"},imgNo+" 获取图书类别列表error",false);
            alert("异常!");
        }
    });

    var auditStatus = $("#auditStatus");
    $.ajax({
        type: "GET",
        url: "/sys/archives/auditStatus",
        dataType: "json",
        success: function (data) {
            if (data != null) {
                auditStatus.html("");
                var options = "<option value=\"0\">请选择</option>";
                for (var i = 0; i < data.length; i++) {
                    options += "<option value=\"" + data[i].id + "\" >" + data[i].valueName + "</option>";
                }
                auditStatus.html(options);
            }
        },
        error: function (data) {
            alert("异常!");
        }
    });

    var education = $("#education");
    var eValue = $("#eValue").val();
    $.ajax({
        type: "GET",
        url: "/sys/archives/education",
        dataType: "json",
        success: function (data) {
            if (data != null) {
                education.html("");
                var options = "<option value=\"0\">请选择</option>";
                for (var i = 0; i < data.length; i++) {
                    if (eValue == data[i].id) {
                        options += "<option value=\"" + data[i].id + "\" selected >" + data[i].valueName + "</option>";
                    } else {
                        options += "<option value=\"" + data[i].id + "\" >" + data[i].valueName + "</option>";
                    }
                }
                education.html(options);
            }
        },
        error: function (data) {
            //validateTip(booktype.next(),{"color":"red"},imgNo+" 获取图书类别列表error",false);
            alert("异常!");
        }
    });

    var salS = $("#salS");
    var salValue = $("#salValue").val();
    $.ajax({
        type: "GET",
        url: "/sys/archives/salS",
        dataType: "json",
        success: function (data) {
            if (data != null) {
                salS.html("");
                var options = "<option value=\"0\">请选择</option>";
                for (var i = 0; i < data.length; i++) {
                    if (salValue == data[i].id) {
                        options += "<option value=\"" + data[i].id + "\" selected >" + data[i].name + "</option>";
                    } else {
                        options += "<option value=\"" + data[i].id + "\" >" + data[i].name + "</option>";
                    }
                }
                salS.html(options);
            }
        },
        error: function (data) {
            //validateTip(booktype.next(),{"color":"red"},imgNo+" 获取图书类别列表error",false);
            alert("异常!");
        }
    });


    $("#add").click(function () {
        window.location.href = "/sys/archives/add";
    });


    function recover(status, id) {
        if (confirm("确定要修改该条数据的档案状态吗？")) {
            $.ajax({
                type: "GET",
                url: "/sys/archives/recover",
                data: {status: status, id: id},
                dataType: "text",
                success: function (data) {
                    if (data == "true") {
                        alert("档案状态修改成功!");
                        window.location.href = "/sys/archives/archivesShow";
                    }
                },
                error: function (data) {
                    //validateTip(booktype.next(),{"color":"red"},imgNo+" 获取图书类别列表error",false);
                    alert("异常!");
                }
            });
        }
    }

    window.recover = recover;


    function del(id) {
        if (confirm("确定要修改该条数据的档案状态吗？")) {
            $.ajax({
                type: "GET",
                url: "/sys/archives/delete",
                data: {id: id},
                dataType: "text",
                success: function (data) {
                    if (data == "true") {
                        alert("永久删除成功!");
                        window.location.href = "/sys/archives/archivesShow";
                    }
                },
                error: function (data) {
                    //validateTip(booktype.next(),{"color":"red"},imgNo+" 获取图书类别列表error",false);
                    alert("异常!");
                }
            });
        }
    }

    window.del = del;

    /*删除附件*/
    function delFile(id, distinguish) {
        if (confirm("确定要删除附件吗？")) {
            $.ajax({
                type: "GET",
                url: "/sys/archives/delFile",
                data: {id: id, distinguish: distinguish},
                dataType: "json",
                success: function (data) {
                    if (data.result == "true") {
                        alert("删除成功!");
                        if (distinguish == 0) {
                            $("#picFile").show();
                            $("#pictureShow").html('');
                        } else {
                            $("#resFile").show();
                            $("#resumeShow").html('');
                        }
                    } else {
                        alert("删除失败!");
                    }
                },
                error: function (data) {
                    alert("异常!");
                }
            });
        }
    }

    window.delFile = delFile;

    /*下载附件*/
    function downloadFile(id) {
        if (confirm("是否下载附件？")) {
            window.location.href = "/sys/archives/downloadFile?id=" + id;
        }
    }

    window.downloadFile = downloadFile;


    var changeType = $("#changeType");
    $.ajax({
        type: "GET",
        url: "/sys/archives/changeType",
        dataType: "json",
        success: function (data) {
            if (data != null) {
                changeType.html("");
                var options = "<option value=\"0\">请选择</option>";
                for (var i = 0; i < data.length; i++) {
                    options += "<option value=\"" + data[i].id + "\" >" + data[i].valueName + "</option>";
                }
                changeType.html(options);
            }
        },
        error: function (data) {
            alert("异常!");
        }
    });


    /**
     * 设置审核,驳回状态
     */
    function setAuditStatus(status) {
        $("#auditStatus").val(status);
    }

    window.setAuditStatus = setAuditStatus;


    var pObj = null;
    var okBtn = $("#ok");

    //获取对象
    function getObj(obj) {
        pObj = obj;
    }

    window.getObj = getObj;

    okBtn.on("click", function () {
        var post = $("input[name='id']:checked");//获取模态框的id选中的对象
        var archives = $(pObj);//将原生态js对象转为jquer对象
        var postId = post.val();//获取模态框的id
        var postName = post.parents("td").next().next().text();//根据对象获取职位名称
        $("#myModal").modal('hide');
        $("#postId").val(postId);
        $("#postName").val(postName);
        getFullName();
    });


    function getFullName() {
        var id = $("#postId").val();
        $.ajax({
            type: "GET",
            url: "/sys/archives/selectFullName",
            data: {id: id},
            dataType: "text",
            success: function (data) {
                if (data != null) {
                    $("#department").val(data);
                }
            },
            error: function (data) {
                alert("异常!");
            }
        });
    }

    window.getFullName = getFullName;


    function changeNum(i) {
        if (i == 0) {
            $("#num").val(8);
        } else {
            $("#num").val(12);
        }
    }

    window.changeNum = changeNum;


    queryList($("#formIndex").get(0), 1);

    queryList($("#formIndex2").get(0), 1);

});
